package com.medical.sys.service.impl;

import com.medical.common.JsonResult;
import com.medical.common.TreeNode;
import com.medical.sys.mapper.ResourceMapper;
import com.medical.sys.mapper.ResourceRoleMapper;
import com.medical.sys.mapper.RoleMapper;
import com.medical.sys.model.Resource;
import com.medical.sys.model.ResourceRole;
import com.medical.sys.model.Role;
import com.medical.sys.service.ResourceService;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@SuppressWarnings("SpringJavaAutowiringInspection")
@Service("resourceService")
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    private ResourceRoleMapper resourceRoleMapper;
    @Autowired
    private RoleMapper roleMapper;


    public List<Role> getRole(String resourceId){
        List<ResourceRole> list=resourceRoleMapper.getrolelist(resourceId);
        List<Role> roles= new ArrayList<Role>();
        for(ResourceRole resourceRole:list){
            roles.add(roleMapper.selectByPrimaryKey(resourceRole.getRoleId()));

        }
        return roles;
    }

    public Resource getResource(String resourceId) {
        return resourceMapper.selectByPrimaryKey(resourceId);
    }

    public List<TreeNode> getList() {
        List<TreeNode> list = new ArrayList<TreeNode>();
        List<Resource> root = getResources();//返回resource表的全部信息
        for (Resource r : root) {
            list.add(toTreeNode(r));
        }
        return list;
    }

    private TreeNode toTreeNode(Resource resource) {
        TreeNode node = new TreeNode();
        node.setId(resource.getId());
        node.setName(resource.getName());
        node.setParentId(resource.getParentId());
        //node.setIcon(resource.getIcon());
        node.setUrl(resource.getUrl());
        if (resource.getType().equals("button")) {
            node.setDrop(false);//不允许拖放到按钮上
        }
        return node;
    }

    public List<Resource> getResources() {
        return getResources(false);
    }

    public List<Resource> getResources(Boolean administrator) {
        return resourceMapper.selectAll(administrator);
    }


    public void saveResource(Resource resource) {
        if (resource != null) {
            if (StringUtils.isEmpty(resource.getId())) {
                resource.setId(UUID.randomUUID().toString());
            }
            for (String roleId : resource.getRoleIds()) {
                resourceRoleMapper.add(resource.getId(), roleId);
            }
            if (StringUtils.isEmpty(resource.getParentId())) {
                resource.setParentId("0");
            }
            Resource parent = getResource(resource.getParentId());
            if (parent != null) {
                resource.setParentIdsStr(parent.getSelfForParent());
            } else {
                resource.setParentIdsStr("0");
            }
            resource.setAvailable(true);
            List<Resource> childrens = resourceMapper.selectByParentId(resource.getParentId());
            resource.setPriority(childrens.size());
            resourceMapper.insert(resource);
        }
    }


    public void updateResource(Resource resource) {
        resourceRoleMapper.remove(resource.getId(), null);
        for (String roleId : resource.getRoleIds()) {
            resourceRoleMapper.add(resource.getId(), roleId);
        }
        Resource parent = getResource(resource.getParentId());
        if (parent != null) {
            resource.setParentIdsStr(parent.getSelfForParent());
        } else {
            resource.setParentIdsStr("0");
        }
        resourceMapper.updateByPrimaryKey(resource);
    }

    public void deleteResource(String resourceId) {
        deleteResource(resourceId, false);
    }

    public void deleteResource(String resourceId, Boolean administrator) {
        if (administrator) {
            resourceMapper.deleteByPrimaryKey(resourceId);
        } else {
            Resource resource = resourceMapper.selectByPrimaryKey(resourceId);
            if (resource != null) {
                resource.setDeleted(true);
                resourceMapper.updateByPrimaryKey(resource);
            }
        }
    }


    public void moveResource(String id, String targetId, String moveType) {
        Resource resource = resourceMapper.selectByPrimaryKey(id);               //要拖拉的目标
        Resource target = resourceMapper.selectByPrimaryKey(targetId);           //拖拉到哪里的目标
        String sourceParentId = resource.getParentId();                          //要拖拉目标的parentid
        String parentId = "0";
        switch (moveType) {
            case "inner":                                                        //拖拉进目标的内部
                parentId = targetId;
                break;
            default:
                if (target != null) {
                    parentId = target.getParentId();
                }
                break;
        }                                                                       //以上就是设置parentid的值
        List<Resource> childrens = resourceMapper.selectByParentId(parentId);   //获取同parentid的Resource对象集合
        if(!parentId.equals(sourceParentId)) {
            resource.setParentId(parentId);
        }
        Integer priority = 0;
        for(Resource children : childrens){
            if(!id.equals(children.getId())) {
                if (!targetId.equals(children.getId())) {
                    children.setPriority(priority);
                    priority++;
                } else {
                    switch (moveType) {
                        case "inner":
                            resource.setPriority(childrens.size());
                            break;
                        case "prev":
                            resource.setPriority(priority);
                            children.setPriority(priority + 1);
                            priority = priority + 2;
                            break;
                        case "next":
                            children.setPriority(priority);
                            resource.setPriority(priority + 1);
                            priority = priority + 2;
                            break;
                    }
                }
            }
            updateResource(children);
        }
        updateResource(resource);
        if (!parentId.equals(sourceParentId)) {//同级移动时不修改旧子集
            List<Resource> oldChildrens = resourceMapper.selectByParentId(sourceParentId);
            for (int j = 0; j < oldChildrens.size(); j++) {
                Resource oldChildren = oldChildrens.get(j);
                oldChildren.setPriority(j);
                updateResource(oldChildren);
            }
        }
    }

    public void removeBySource(List<Resource> list, Resource source) {
        for (Resource r : list) {
            if (r.getId().equals(source.getId())) {
                list.remove(r);
                break;
            }
        }
    }

    public List<Resource> getResourcesByRoleId(String roleId) {
        return resourceMapper.selectByRoleId(roleId);
    }

    public List<Resource> getResourceList(){
        List<Resource> resmap=new ArrayList<>();
        List<Resource> resources=resourceMapper.selectAll(true);
        List<Resource> parents=resourceMapper.selectByParentId("0");
        for(Resource parent :parents){
            List<Resource> parentss=parent.getChilden();
            for(Resource resource:resources){
                if(resource.getParentId().equals(parent.getId())){
                    parentss.add(resource);
                }
            }
            parent.setChilden(parentss);
            resmap.add(parent);
        }
        return resmap;
    }

}

